# -*- coding: utf-8 -*-
from scrapy.exceptions import DropItem

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

from scrapy.pipelines.images import ImagesPipeline
from scrapy.utils.project import get_project_settings
import os
import logging
logger = logging.getLogger('dmoz')


class MyImagesPipeline(ImagesPipeline):
    def item_completed(self, results, item, info):
        for result in [x for ok, x in results if ok]:
            path = result['path']
            title = item['title'][0]

            settings = get_project_settings()
            storage = settings.get('IMAGES_STORE')

            target_path = os.path.join(storage, title, os.path.basename(path))
            path = os.path.join(storage, path)

            # If path doesn't exist, it will be created
            if not os.path.exists(os.path.join(storage, title)):
                os.makedirs(os.path.join(storage, title))

            os.rename(path, target_path)

        if self.IMAGES_RESULT_FIELD in item.fields:
            item[self.IMAGES_RESULT_FIELD] = [x for ok, x in results if ok]
        return item
